// MyTabCtrl.cpp : implementation file
//
/////////////////////////////////////////////////////
// This class is provided as is and Ben Hill takes no
// responsibility for any loss of any kind in connection
// to this code.
/////////////////////////////////////////////////////
// Is is meant purely as a educational tool and may
// contain bugs.
/////////////////////////////////////////////////////
// ben@shido.fsnet.co.uk
// http://www.shido.fsnet.co.uk
/////////////////////////////////////////////////////
// Thanks to a mystery poster in the C++ forum on 
// www.codeguru.com I can't find your name to say thanks
// for your Control drawing code. If you are that person 
// thank you very much. I have been able to use some of 
// you ideas to produce this sample application.
/////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyTabCtrl.h"
#include "SenderDialog.h"
#include "PopupPropertiesDlg.h"
#include "PopupHistoryDialog.h"
#include "Popup.h"
#include "PopupDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyTabCtrl

CMyTabCtrl::CMyTabCtrl() :
	m_senderDialog(new CSenderDialog()), 
	m_historyDialog(new PopupHistoryDialog()),
	m_pptiesDialog(new PopupPropertiesDlg())
{
	m_tabPages[POPUP_SENDER_TAB]= m_senderDialog;
	m_tabPages[POPUP_HISTORY_TAB]= m_historyDialog;
	m_tabPages[POPUP_PPTIES_TAB]= m_pptiesDialog;
	m_nNumberOfPages = POPUP_NB_TABS;
}

CMyTabCtrl::~CMyTabCtrl()
{
	for(int nCount=0; nCount < m_nNumberOfPages; nCount++){
		delete m_tabPages[nCount];
	}
}

void CMyTabCtrl::Init()
{
	InsertItem(POPUP_SENDER_TAB, _T("[SEND A MESSAGE]"));
	InsertItem(POPUP_HISTORY_TAB, _T("[HISTORY]"));
	InsertItem(POPUP_PPTIES_TAB, _T("[PROPERTIES]"));

	m_tabCurrent=0;

	m_tabPages[POPUP_SENDER_TAB]->Create(IDD_DIALOG_SENDEVENT, this);
	m_tabPages[POPUP_SENDER_TAB]->ShowWindow(SW_SHOW);
	m_tabPages[POPUP_HISTORY_TAB]->Create(IDD_DIALOG_HISTORY, this);
	m_tabPages[POPUP_HISTORY_TAB]->ShowWindow(SW_HIDE);
	m_tabPages[POPUP_PPTIES_TAB]->Create(IDD_DIALOG_PROPERTIES, this);
	m_tabPages[POPUP_PPTIES_TAB]->ShowWindow(SW_HIDE);
	
	SetRectangle();
}

void CMyTabCtrl::SetRectangle()
{
	CRect tabRect, itemRect;
	int nX, nY, nXc, nYc;

	GetClientRect(&tabRect);
	GetItemRect(0, &itemRect);

	nX=itemRect.left;
	nY=itemRect.bottom+1;
	nXc=tabRect.right-itemRect.left-1;
	nYc=tabRect.bottom-nY-1;

	m_tabPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);
	for(int nCount=1; nCount < m_nNumberOfPages; nCount++){
		m_tabPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);
	}
}

BEGIN_MESSAGE_MAP(CMyTabCtrl, CTabCtrl)
	//{{AFX_MSG_MAP(CMyTabCtrl)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTabCtrl message handlers

BOOL CMyTabCtrl::btnOkClicked()
{
	CDialog *_dlg = m_tabPages[m_tabCurrent];
	CSenderDialog *_sender = 0;
	PopupPropertiesDlg *_ppties = 0;
	PopupHistoryDialog *_hist = 0;

	if ((_sender = dynamic_cast<CSenderDialog*>(_dlg)) != 0)
	{
		return _sender->validateDialog();
	}
	else if ((_ppties = dynamic_cast<PopupPropertiesDlg*>(_dlg)) != 0)
	{
		return _ppties->validateDialog();
	}
	else if ((_hist = dynamic_cast<PopupHistoryDialog*>(_dlg)) != 0)
	{
		return _hist->validateDialog();
	}

	return FALSE;
}

void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CTabCtrl::OnLButtonDown(nFlags, point);
	setActiveTab(GetCurFocus());
}

void CMyTabCtrl::setActiveTab(int p_tabId)
{
	CTabCtrl::SetCurSel(p_tabId);
	if (m_tabCurrent != p_tabId)
	{
		m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
		m_tabCurrent = p_tabId;
		m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
		m_tabPages[m_tabCurrent]->SetFocus();
	}
}
